var count_question = 1;
var process_bar = new Array('6%','12%','18%','25%','31%','37%','43%','50%','56%','62%','68%','75%','81%','87%','93%');//15câu hỏi, 93% là chỗ choose ten, khi đăng ký email xong nó chuyển tới trang chọn lại tên vì tên đó có rồi, chọn xong quay lại email nếu email đã có rồi, ok hết thì chạy tới trang tiếp theo
var has_answer = new Array(false,false,false,false,false,false,false,false,false,false,false,false,false,false,false);
var word_error = new Array('Please choose your Gender.',
						'Please choose your Hair Color.',
						'Please choose your Eye Color.',
						'Please choose your Height.',
						'Please choose your Ethnicity.',
						'What is your body type?',
						'Please choose your Marital Status.',
						'Please choose your Number of Children.',
						'Please choose your Living at Home.',
						'Please choose your Smoking Preference.',//10
						'Please select a country.'
						);
word_error[11] = new Array('Please enter a valid zip/postal code.','The specified postal code is not valid.','Please enter a city.<br>Please enter a state.','Please enter a city.','Please enter a state.');
word_error[12] = new Array('Please enter your complete birthday.','You must be past your 18th birthday to participate in this site.');
word_error[13] = new Array('Please enter a username.<br/>Please enter your password.','Please enter a username.','Please enter your password.','Username must be between 4-16 characters - Try again.','Password must be between 4-16 characters - Try again.')
word_error[14] = new Array('Please enter an email address.<br>You must agree to the Terms and Conditions.','Please enter a valid email address.','Email addresses do not match.','You must agree to the Terms and Conditions.');
var last_answer = 0;
$(document).ready(function(){
	
	/*
	* Description: Continue questions
	* author: Khanh Nguyen
	*/
	$('#common_body #funnel_content .continuebtn').click(function(){
		if($(this).parent().parent().parent().find('.profileresponse_selected').html()!== undefined){			
			if(count_question < 10){
				$('#common_body #funnel_content #divProfile').css('display','block');
				$('#common_body #funnel_content #divLoginRegistration').css('display','none');
			}else{
				$('#common_body #funnel_content #divProfile').css('display','none');
				$('#common_body #funnel_content #divLoginRegistration').css('display','block');						
			}
            
			$(this).parent().parent().parent().css('display','none');
			$(this).parent().parent().parent().next().css('display','block');
			$('#funnel_bar_outter #funnel_bar_inner .progressbarouter .progressbarinner img').css('width',process_bar[count_question]);
			$('#funnel_bar_outter #funnel_bar_inner .progressbarouter .percentageplace .percentage #percentagevalue').html(process_bar[count_question]);			
			count_question++;
			update_last_answer();
			if(count_question>1){				
				$('#funnel_topper #funnel_bar_container .divProfilePrevious').css('display','block');
			}
		}else{			
			var error = return_error(word_error[count_question-1]);
			if(count_question < 10){
				remove_error($(this).parent().parent());
				$(error).insertAfter($(this).parent().parent().find('.divQuestion'));
			}else{
				remove_error($(this).parent().parent().parent());
				$(error).insertAfter($(this).parent().parent().parent().find('.divQuestion'));
			}
		}
	});
	
	/*
	* Description: Continue questions city/state/zipcode
	* author: Khanh Nguyen
	*/
	$('#common_body #funnel_content .continuebtn2').click(function(){
		check = checkCountry();
		if(check == -1){
			remove_error($(this).parent());
			$(this).parent().css('display','none');
			$(this).parent().next().css('display','block');
			$('#funnel_bar_outter #funnel_bar_inner .progressbarouter .progressbarinner img').css('width',process_bar[count_question]);
			$('#funnel_bar_outter #funnel_bar_inner .progressbarouter .percentageplace .percentage #percentagevalue').html(process_bar[count_question]);
			count_question++;
			update_last_answer();
		}else{
			var error = return_error(word_error[11][check]);
			remove_error($(this).parent());
			$(error).insertAfter($(this).parent().find('.divQuestion'));
		}
	});
	
	/*
	* Description: Continue questions Date Of Birth
	* author: Khanh Nguyen
	*/
	$('#common_body #funnel_content .continuebtn3').click(function(){
		check = checkDateOfBirth($('#ddlBirthMonth').val(),$('#ddlBirthDay').val(),$('#ddlBirthYear').val());
		if(check==-1){
			remove_error($(this).parent());
			$(this).parent().css('display','none');
			$(this).parent().next().css('display','block');
			$('#funnel_bar_outter #funnel_bar_inner .progressbarouter .progressbarinner img').css('width',process_bar[count_question]);
			$('#funnel_bar_outter #funnel_bar_inner .progressbarouter .percentageplace .percentage #percentagevalue').html(process_bar[count_question]);
			count_question++;
			update_last_answer();
		}else{
			var error = return_error(word_error[12][check]);
			remove_error($(this).parent());
			$(error).insertAfter($(this).parent().find('.divQuestion'));
		}
	});
	
	/*
	* Description: Continue questions Username Password
	* author: Khanh Nguyen
	*/
	$('#common_body #funnel_content .continuebtn4').click(function(){	
		check = checkUserPass();
		if(check==-1){
			remove_error($(this).parent().parent());
			$(this).parent().parent().css('display','none');
			$(this).parent().parent().next().css('display','block');
			$('#funnel_bar_outter #funnel_bar_inner .progressbarouter .progressbarinner img').css('width',process_bar[count_question]);
			$('#funnel_bar_outter #funnel_bar_inner .progressbarouter .percentageplace .percentage #percentagevalue').html(process_bar[count_question]);
			count_question++;
			update_last_answer();
		}else{
			var error = return_error(word_error[13][check]);
			remove_error($(this).parent().parent());
			$(error).insertAfter($(this).parent().parent().find('.divQuestion'));
		}
	});
	
	/*
	* Description: Continue questions Email
	* author: Khanh Nguyen
	*/
	$('#common_body #funnel_content .continuebtn5').click(function(){
		check = checkEmail();
		if(check == -1){
			remove_error($(this).parent().parent());
            $('#register').submit();
			/*$(this).parent().css('display','none');
			$(this).parent().next().css('display','block');
			$('#funnel_bar_outter #funnel_bar_inner .progressbarouter .progressbarinner img').css('width',process_bar[count_question]);
			$('#funnel_bar_outter #funnel_bar_inner .progressbarouter .percentageplace .percentage #percentagevalue').html(process_bar[count_question]);
			count_question++;
			update_last_answer();*/
		}else{
			var error = return_error(word_error[14][check]);
			remove_error($(this).parent().parent());
			$(error).insertAfter($(this).parent().parent().find('.divQuestion'));
		}
	});
	
	/*
	* Description: Click in question
	* author: Khanh Nguyen
	*/
	$('#common_body #funnel_content .divresponse .profileresponsecontainer .profileresponse').click(function(){	
		remove_error($(this).parent().parent().parent().parent().parent());
		$(this).parent().find('.profileresponse').removeClass('profileresponse_selected');
		$(this).addClass('profileresponse_selected');
		if(count_question < 10){
			$('#common_body #funnel_content #divProfile').css('display','block');
			$('#common_body #funnel_content #divLoginRegistration').css('display','none');
			//alert($(this).parent().parent().parent().parent().html());
			$(this).parent().parent().parent().parent().parent().css('display','none');
			$(this).parent().parent().parent().parent().parent().next().css('display','block');
            
		}else{
			$('#common_body #funnel_content #divProfile').css('display','none');
			$('#common_body #funnel_content #divLoginRegistration').css('display','block');
		}
		var value_id = $(this).attr('id');
        value_id = value_id.split("_");
        value_id = value_id[value_id.length-1];  
        $(this).parent().parent().find('input').val(value_id);
		$('#funnel_bar_outter #funnel_bar_inner .progressbarouter .progressbarinner img').css('width',process_bar[count_question]);
		$('#funnel_bar_outter #funnel_bar_inner .progressbarouter .percentageplace .percentage #percentagevalue').html(process_bar[count_question]);
		count_question++;
		update_last_answer();
		if(count_question>1){				
			$('#funnel_topper #funnel_bar_container .divProfilePrevious').css('display','block');
		}
	});
});

/*
* Description: Click in question country
* author: Khanh Nguyen
*/
function showZipPostalCodeCityStatePanel(country){	
	remove_error($('#ddlCountryItem'+country).parent().parent().parent().parent());
	$('#ddlCountryItem'+country).parent().find('.countryresponse').removeClass('profileresponse_selected');	//divPostalCode,divCityState
	$('#ddlCountryItem'+country).addClass('profileresponse_selected');
	
	$('#ddlCountryItem'+country).parent().parent().parent().parent().css('display','none');
	$('#ddlCountryItem'+country).parent().parent().parent().parent().next().css('display','block');
	if(country == 'US' || country == 'CA'){
		$('#ddlCountryItem'+country).parent().parent().parent().parent().next().find('#divPostalCode').css('display','block');
		$('#ddlCountryItem'+country).parent().parent().parent().parent().next().find('#divCityState').css('display','none');
	}else{
		$('#ddlCountryItem'+country).parent().parent().parent().parent().next().find('#divPostalCode').css('display','none');
		$('#ddlCountryItem'+country).parent().parent().parent().parent().next().find('#divCityState').css('display','block');
	}
	$('#ddlCountry').val(country);
	$('#funnel_bar_outter #funnel_bar_inner .progressbarouter .progressbarinner img').css('width',process_bar[count_question]);
	$('#funnel_bar_outter #funnel_bar_inner .progressbarouter .percentageplace .percentage #percentagevalue').html(process_bar[count_question]);
	count_question++;
	update_last_answer();
}

/*
* Description: back question
* author: Khanh Nguyen
*/
function showPrevPanel(){
	//if(count_question<1)
	if(count_question<=10 && count_question>1){
		$('#divProfileQuestions_'+count_question).css('display','none');
		$('#divProfileQuestions_'+(count_question-1)).css('display','block');
		count_question--;
		$('#funnel_bar_outter #funnel_bar_inner .progressbarouter .progressbarinner img').css('width',process_bar[count_question-1]);
		$('#funnel_bar_outter #funnel_bar_inner .progressbarouter .percentageplace .percentage #percentagevalue').html(process_bar[count_question-1]);
	}else if(count_question==11){		
		$('#common_body #funnel_content #divProfile').css('display','block');
		$('#divProfileQuestions_10').css('display','block');
		$('#common_body #funnel_content #divLoginRegistration').css('display','none');
		count_question--;
		$('#funnel_bar_outter #funnel_bar_inner .progressbarouter .progressbarinner img').css('width',process_bar[count_question-1]);
		$('#funnel_bar_outter #funnel_bar_inner .progressbarouter .percentageplace .percentage #percentagevalue').html(process_bar[count_question-1]);
	}else if(count_question>11){
		$('#divRegistrationQuestions_'+(count_question-10)).css('display','none');
		$('#divRegistrationQuestions_'+(count_question-10-1)).css('display','block');
		count_question--;
		$('#funnel_bar_outter #funnel_bar_inner .progressbarouter .progressbarinner img').css('width',process_bar[count_question-1]);
		$('#funnel_bar_outter #funnel_bar_inner .progressbarouter .percentageplace .percentage #percentagevalue').html(process_bar[count_question-1]);
	}
	$('#funnel_topper #funnel_bar_container .divProfileNext').css('display','block');
}

/*
* Description: next question
* author: Khanh Nguyen
*/
function showNextPanel(){
	//alert(count_question+':'+last_answer);
	if(count_question<last_answer){
		if(count_question<10 && count_question>0){
			$('#divProfileQuestions_'+count_question).css('display','none');
			$('#divProfileQuestions_'+(count_question+1)).css('display','block');
			count_question++;
			$('#funnel_bar_outter #funnel_bar_inner .progressbarouter .progressbarinner img').css('width',process_bar[count_question-1]);
			$('#funnel_bar_outter #funnel_bar_inner .progressbarouter .percentageplace .percentage #percentagevalue').html(process_bar[count_question-1]);
		}else if(count_question==10){		
			$('#common_body #funnel_content #divProfile').css('display','none');
			$('#divRegistrationQuestions_1').css('display','block');
			$('#common_body #funnel_content #divLoginRegistration').css('display','block');
			count_question++;
			$('#funnel_bar_outter #funnel_bar_inner .progressbarouter .progressbarinner img').css('width',process_bar[count_question-1]);
			$('#funnel_bar_outter #funnel_bar_inner .progressbarouter .percentageplace .percentage #percentagevalue').html(process_bar[count_question-1]);
		}else if(count_question>10){
			$('#divRegistrationQuestions_'+(count_question-10)).css('display','none');
			$('#divRegistrationQuestions_'+(count_question-10+1)).css('display','block');
			count_question++;
			$('#funnel_bar_outter #funnel_bar_inner .progressbarouter .progressbarinner img').css('width',process_bar[count_question-1]);
			$('#funnel_bar_outter #funnel_bar_inner .progressbarouter .percentageplace .percentage #percentagevalue').html(process_bar[count_question-1]);
		}
	}
}

function update_last_answer(){	
	if(count_question>last_answer){
		last_answer = count_question;
	}
}

/*
* Description: return format error
* author: Khanh Nguyen
*/
function return_error(strError){	
	$error = '<div class="errorMessageSummary newSignupError" style="display: block;"><ul><li>'+strError+'</li></ul></div>';
	$error += '<div style="display: block;" class="accounterrorarrow"> <img src="http://pmi.peoplemedia.com/V4/21/images/external/errorarrow.png"> </div>';
	return $error;
}

function remove_error(this1){
	$(this1).find('.errorMessageSummary').remove();
	$(this1).find('.accounterrorarrow').remove();
}

function checkDateOfBirth(month, day, year){
	if(month==0 || day==0 || year==0){
		return 0;
	}else{
		age = getAge(year+'/'+month+'/'+day);
		if(age>=18){
			return -1;
		}else{
			return 1;
		}
	}
	return 0;
}

function getAge(dateString)
{
    var today = new Date();
    var birthDate = new Date(dateString);
    var age = today.getFullYear() - birthDate.getFullYear();
    var m = today.getMonth() - birthDate.getMonth();
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate()))
    {
        age--;
    }
    return age;
} 

function checkCountry(){
	if($('#divPostalCode').css('display')=='none'){
        //alert("ABC");
		if($('#txtCity').val()=='' && $('#txtState').val()==''){	
			return 2;
		}else if($('#txtCity').val()=='' && $('#txtState').val()!=''){
			return 3;
		}else if($('#txtCity').val()!='' && $('#txtState').val()==''){
			return 4;
		}else{
			return -1;
		}
	}else{
		if($('#txtPostalCode').val()==''){		//check zip code in internet
			return 0;
		}else if(isNaN(Number($('#txtPostalCode').val())) || $('#txtPostalCode').val().length<5 || $('#txtPostalCode').val().length>5){
			return 1
		}else{
			return -1;
		}
	}
	return 0;
}

function checkUserPass(){
	if($('#txtUsername').val()=='' && $('#txtPassword').val()==''){
		return 0;
	}else if($('#txtUsername').val()=='' && $('#txtPassword').val()!=''){
		return 1;
	}else if($('#txtUsername').val()!='' && $('#txtPassword').val()==''){
		return 2;
	}
	if($('#txtUsername').val()!=''){
		if($('#txtUsername').val().length<4 || $('#txtUsername').val().length>16){
			return 3;
		}
	}
	if($('#txtPassword').val()!=''){
		if($('#txtPassword').val().length<4 || $('#txtPassword').val().length>16){
			return 4;
		}
	}
	return -1;
}

function checkEmail(){
	if($('#txtEmailAddress').val()==''){
		return 0;
	}else if(validateEmail($('#txtEmailAddress').val())==false){
		return 1;
	}else if($('#txtEmailAddressConfirmation').val() != $('#txtEmailAddress').val()){
		return 2;
	}else if ($('#chkRegTermsAndConditions').is(':checked')==false) {
		return 3;
	}else{
		return -1;
	}
}

function validateEmail(email) { 
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
} 